﻿@using Volo.Abp.SettingManagement.Localization
@inherits Volo.Abp.AspNetCore.Components.AbpComponentBase
@inject AbpBlazorMessageLocalizerHelper<AbpSettingManagementResource> LH

@if (EmailSettings != null)
{
    <Form>
        <Validations @ref="@EmailSettingValidation" Model="@EmailSettings" ValidateOnLoad="false">
            <Row>
                <Column>
                    <Validation MessageLocalizer="@LH.Localize">
                        <Field>
                            <FieldLabel>@L["DefaultFromDisplayName"] *</FieldLabel>
                            <TextEdit @bind-Text="@EmailSettings.DefaultFromDisplayName">
                                <Feedback>
                                    <ValidationError/>
                                </Feedback>
                            </TextEdit>
                        </Field>
                    </Validation>
                    <Validation MessageLocalizer="@LH.Localize">
                        <Field>
                            <FieldLabel>@L["DefaultFromAddress"] *</FieldLabel>
                            <TextEdit @bind-Text="@EmailSettings.DefaultFromAddress">
                                <Feedback>
                                    <ValidationError/>
                                </Feedback>
                            </TextEdit>
                        </Field>
                    </Validation>
                    <Validation MessageLocalizer="@LH.Localize">
                        <Field>
                            <FieldLabel>@L["SmtpHost"]</FieldLabel>
                            <TextEdit @bind-Text="@EmailSettings.SmtpHost">
                                <Feedback>
                                    <ValidationError/>
                                </Feedback>
                            </TextEdit>
                        </Field>
                    </Validation>
                    <Validation MessageLocalizer="@LH.Localize">
                        <Field>
                            <FieldLabel>@L["SmtpPort"]</FieldLabel>
                            <NumericEdit TValue="int" @bind-Value="@EmailSettings.SmtpPort">
                                <Feedback>
                                    <ValidationError/>
                                </Feedback>
                            </NumericEdit>
                        </Field>
                    </Validation>
                    <Field>
                        <Check TValue="bool" @bind-Checked="@EmailSettings.SmtpEnableSsl">@L["SmtpEnableSsl"]</Check>
                    </Field>
                    <Field>
                        <Check TValue="bool" @bind-Checked="@EmailSettings.SmtpUseDefaultCredentials">@L["SmtpUseDefaultCredentials"]</Check>
                    </Field>
                    @if (!EmailSettings.SmtpUseDefaultCredentials)
                    {
                        <Validation MessageLocalizer="@LH.Localize">
                            <Field>
                                <FieldLabel>@L["SmtpDomain"]</FieldLabel>
                                <TextEdit @bind-Text="@EmailSettings.SmtpDomain">
                                    <Feedback>
                                        <ValidationError/>
                                    </Feedback>
                                </TextEdit>
                            </Field>
                        </Validation>
                        <Validation MessageLocalizer="@LH.Localize">
                            <Field>
                                <FieldLabel>@L["SmtpUserName"]</FieldLabel>
                                <TextEdit @bind-Text="@EmailSettings.SmtpUserName">
                                    <Feedback>
                                        <ValidationError/>
                                    </Feedback>
                                </TextEdit>
                            </Field>
                        </Validation>
                        <Validation MessageLocalizer="@LH.Localize">
                            <Field>
                                <FieldLabel>@L["SmtpPassword"]</FieldLabel>
                                <TextEdit Role="TextRole.Password" @bind-Text="@EmailSettings.SmtpPassword" Placeholder="@L["SmtpPasswordPlaceholder"].Value">
                                    <Feedback>
                                        <ValidationError/>
                                    </Feedback>
                                </TextEdit>
                            </Field>
                        </Validation>
                    }
                </Column>
            </Row>
        </Validations>
        <hr />
        <Row>
            <Column>
                @if (HasSendTestEmailPermission)
                {
                    <Button style="margin: 0.25rem" Color="Color.Primary" Outline Clicked="OpenSendTestEmailModalAsync">@L["SendTestEmail"]</Button>
                }
                <SubmitButton Clicked="@UpdateSettingsAsync"/>
            </Column>

        </Row>
    </Form>

    @if (HasSendTestEmailPermission)
    {
        <Modal @ref="SendTestEmailModal">
            <ModalContent Centered="true">
                <Form>
                    <ModalHeader>
                        <ModalTitle>@L["SendTestEmail"]</ModalTitle>
                        <CloseButton Clicked="CloseSendTestEmailModalAsync"/>
                    </ModalHeader>
                    <ModalBody>
                        <Validations @ref="@EmailSettingTestValidation" Model="@SendTestEmailInput" ValidateOnLoad="false">
                            <Validation MessageLocalizer="@LH.Localize">
                                <Field>
                                    <FieldLabel>@L["SenderEmailAddress"]</FieldLabel>
                                    <TextEdit @bind-Text="@SendTestEmailInput.SenderEmailAddress" Autofocus="true">
                                        <Feedback>
                                            <ValidationError/>
                                        </Feedback>
                                    </TextEdit>
                                </Field>
                            </Validation>
                            <Validation MessageLocalizer="@LH.Localize">
                                <Field>
                                    <FieldLabel>@L["TargetEmailAddress"]</FieldLabel>
                                    <TextEdit @bind-Text="@SendTestEmailInput.TargetEmailAddress">
                                        <Feedback>
                                            <ValidationError/>
                                        </Feedback>
                                    </TextEdit>
                                </Field>
                            </Validation>
                            <Validation MessageLocalizer="@LH.Localize">
                                <Field>
                                    <FieldLabel>@L["Subject"]</FieldLabel>
                                    <TextEdit @bind-Text="@SendTestEmailInput.Subject">
                                        <Feedback>
                                            <ValidationError/>
                                        </Feedback>
                                    </TextEdit>
                                </Field>
                            </Validation>
                            <Validation MessageLocalizer="@LH.Localize">
                                <Field>
                                    <FieldLabel>@L["Body"]</FieldLabel>
                                    <TextEdit @bind-Text="@SendTestEmailInput.Body">
                                        <Feedback>
                                            <ValidationError/>
                                        </Feedback>
                                    </TextEdit>
                                </Field>
                            </Validation>
                        </Validations>
                    </ModalBody>
                    <ModalFooter>
                        <Button Color="Color.Secondary" Clicked="CloseSendTestEmailModalAsync">@L["Cancel"]</Button>
                        <Button Color="Color.Primary" Clicked="SendTestEmailAsync">@L["Send"]</Button>
                    </ModalFooter>
                </Form>
            </ModalContent>
        </Modal>
    }
}
